package com.itheima.mp.controller;


import com.itheima.mp.domain.po.JosnTest;
import com.itheima.mp.service.IJosnTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-03
 */
@RestController
@RequestMapping("/josn-test")
public class JosnTestController {

    @Autowired
    private IJosnTestService josnTestService;

    // GET请求：获取所有JosnTest信息
    @GetMapping("/list")
    public List<JosnTest> getAllJosnTests() {
        return josnTestService.list();
    }

    // GET请求：根据ID获取JosnTest信息
    @GetMapping("/{id}")
    public JosnTest getJosnTestById(@PathVariable String id) {
        return josnTestService.getById(id);
    }

    // GET请求：生成并添加两个JosnTest信息
    @GetMapping("/add-two")
    public String addTwoJosnTests() {
        try {
            // 创建两个JosnTest实例
            JosnTest josnTest1 = new JosnTest();
            josnTest1.setJosnData("Data for JosnTest 1");
            josnTest1.setCreateTime(LocalDateTime.now());
            josnTest1.setUpdateTime(LocalDateTime.now());

            JosnTest josnTest2 = new JosnTest();
            josnTest2.setJosnData("Data for JosnTest 2");
            josnTest2.setCreateTime(LocalDateTime.now());
            josnTest2.setUpdateTime(LocalDateTime.now());

            // 保存两个JosnTest实例
            boolean result1 = josnTestService.save(josnTest1);
            boolean result2 = josnTestService.save(josnTest2);

            // 检查两个实例是否都成功保存
            if (result1 && result2) {
                return "Successfully added two JosnTest records";
            } else {
                return "Failed to add one or both JosnTest records";
            }
        } catch (Exception e) {
            return "Error occurred: " + e.getMessage();
        }
    }

    // POST请求：添加JosnTest信息
    @PostMapping("/add")
    public boolean addJosnTest(@RequestBody JosnTest josnTest) {
        return
                josnTestService.save(josnTest);
    }
}
